/*==============================================================================
MASTER DO-FILE FOR : "MANDATORY NOTICE OF LAYOFF, JOB SEARCH, AND EFFICIENCY" BY
JONAS CEDERLÖF, PETER FREDRIKSSON, ARASH NEKOEI AND DAVID SEIM.
==============================================================================*/


*Set working directory
cd "/home/projects/cedsei/do/"

*Set global paths
*Data
global rawdatapath 	"../data/rawdata"
global rawdatapath2	"../../../../data2/cedsei/data2"
global datapath		"../data"

*Output
global agegraphpath 	"../output5/Ind/graphs"
global agetablepath 	"../output5/Ind/tables"
global firmgraphpath	"../output5/Firm/graphs" 
global firmtablepath	"../output5/Firm/tables"

{ // Program Define (Fastmax)
********************************************************************************
*Description: The purpose of the program is to speed up the max function
*			  and abstain from using the much slower egen (max) command.
capture program drop fastmax fastmin

program define fastmax, rclass
	syntax varlist [if] [in] , [by(varlist)] name(string)

	tempvar maxvar
	clonevar `maxvar' = `varlist'

	if  "`by'" != "" {
		bys `by' : 	replace `maxvar' = max( `maxvar'[_n-1], `maxvar') 
		bys `by' : 	gen  	`name'   =  `maxvar'[_N] 
	}
	else {
					replace `maxvar' = max( `maxvar'[_n-1], `maxvar') 
					gen  	`name'   = `maxvar'[_N] 
}
end

program define fastmin, rclass
	syntax varlist [if] [in] , [by(varlist)] name(string)

	tempvar maxvar
	clonevar `maxvar' = `varlist'

	if  "`by'" != "" {
		bys `by' : 	replace `maxvar' = min( `maxvar'[_n-1], `maxvar') 
		bys `by' : 	gen  	`name'   =  `maxvar'[_N] 
	}
	else {
					replace `maxvar' = min( `maxvar'[_n-1], `maxvar') 
					gen  	`name'   = `maxvar'[_N] 
}
end



}
*


********************************************************
******************* DATA PREPERATION *******************
********************************************************
// A0: 	Creating supporting datasets and appending
do 	"A0/A0_preparatory.do"
do 	"A0/A0_persid_varsel.do"				// Creates a list of all indivuals ever getting notified
do  "A0/A0_fodelsedatum.do" 				// Create assisting dataset with birth year and month for indivuals. 
do 	"A0/A0_append_LISAind.do"				// Appends 2000-2018 LISA indivual (entire population)
do 	"A0/A0_clean_jobb2019.do"				// Clean RAMS year 2019 which has a slighlty different format
do  "A0/A0_append_RAMS.do"	 				// Appends 1985-2019 year of RAMS data. Only restricting attention to indivuals present in the merged varsel data.
do 	"A0/A0_append_SSYK.do"					// Append  2001-2015 SSYK (occupation) data
do	"A0/A0_append_wagesurvey.do"			// Appends 2000-2018 wage survey data.   (entire population)	
do	"A0/A0_balancesheetdata.do"				// Takes out variables from balancesheet data which is used in B2_newfirm_v2_DS.do
do 	"A0/A0_tenure_for_all.do"				// Calculate tenure for all workers in RAMS (used in B2_workerstat.do)
do	"A0/A0_AKU.do"							// Clean and fix AKU survey
do 	"A0/A0_histaktso.do"					// Clean and fix Histakso data

// A1: 	Cleaning data
do 	"A1_cleaning/A1_clean_varselorsak.do"		// Clean reason for notification
do  "A1_cleaning/A1_clean_varseldata.do"		// Clean notification data
do 	"A1_cleaning/A1_clean_wagestruc.do"			// Generate two datasets: i) Work category (to be used to create withe and blue collar) ii) All other wage survey variabels
do 	"A1_cleaning/A1_clean_firmchar.do"			// Generate firm characteristics from RAMS data (i.e indiviual data)
do  "A1_cleaning/A1_clean_sokatper.do"			// Cleans and calculates number of days unemployed each month and each year.
do  "A1_cleaning/A1_clean_RAMS.do" 	  			// Collapse to uniqe perid/firmid/year/month combintations and fix monthly indicators

// A2: Merge and generate final data
do 	"A2_merge_and_gen/A2_merge_RAMS_varsel.do"	// Merge RAMS data with notification data, creating a panel for each indivual and notification
do 	"A2_merge_and_gen/A2_gen_tenure.do"			// Generate 5 different meassures of tenure and startdate for notifed workers
do 	"A2_merge_and_gen/A2_gen_earnings.do"		// Generate earnings and "wages" from notifying, other and all firms  
do 	"A2_merge_and_gen/A2_gen_employment.do"		// Generate employment indicators for notifying, other and all firms
do 	"A2_merge_and_gen/A2_complie_all_data.do"	// Merge on datasets and complie final data

// A3: Generte varaibles
do	"A3_generate/A3_WCindicator.do"				// Generate white-collar indicator and save in own dataset
do 	"A3_generate/A3_generate.do"				// Generate some additional variables to final data


********************************************************
******************* AGE DISCONTINUITY ******************
********************************************************
// B1: Preparation of Age data
do "B1_prep_agedata/B1_employment_markers.do"				// Code employment makers and define first new job after notification and merge on wages for those jobs
do "B1_prep_agedata/B1_employment_markers_rel_ankomst.do"	// Code employment makers and define first new job after first layoff resport and merge on wages for those jobs
do "B1_prep_agedata/B1_prep_agedata_WC.do"					// Preperation of age discontinuity data
do "B1_prep_agedata/B1_prep_agedata_WC_rdrobust.do"			// Preperation of age discontinuity data without bandwidth restrictions


// B2: Analysis
do "B2_analysis/B2_balance.do"						// Balancing and density tests
do "B2_analysis/B2_firststage.do"					// First stage estimates (Table and graphs)
do "B2_analysis/B2_workerstat.do"					// Summary statistics for workers and firms
do "B2_analysis/B2_RFestimation.do"					// Estiamtes on employment outcomes and earnings (Table)
do "B2_analysis/B2_severance_prep.do"				// Calculates severance pay and winsorize among all workers
do "B2_analysis/B2_severance.do"					// Graphs of severance (also by CC) and Pr(recall)
do "B2_analysis/B2_wages.do"						// Wage table, RD graph and distribution effect graph
do "B2_analysis/B2_wages_robust.do"					// Robustness on wage estimates
do "B2_analysis/B2_EtoE.do"							// Estimtes and graphs of EtoE transitions
do "B2_analysis/B2_newfirm.do"						// Table with estimates on characterisics of new firm and graph showing probaility of remaning at new firm
do "B2_analysis/B2_search.do"						// Produce search graphs
do "B2_analysis/B2_wage_haz_search_fullsample.do"	// Produce search table
do "B2_analysis/B2_bunching.do"						// Produce bunching estimates
do "B2_analysis/B2_delay.do"						// Produce estiamtes on the cost of delay

// B3: Figures
do "B3_figures/B3_graph_event_RD.do"				// Dynamic graphs on annual earnings, wage and employment
do "B3_figures/B3_graph_RD.do"						// RD graphs on annual earnings and employment outcomes

// B4: Robustness
do "B4_robustness/B4_Obandwidth_graph.do"			// Graph employment outcomes with optimal bandwidthbandwidth restrction
do "B4_robustness/B4_Obandwidth_table.do"			// Table with results  with optimal bandwidthbandwidth restrction
do "B4_robustness/B4_SP_data.do"					// Generate data for checking robustness of SP result	
do "B4_robustness/B4_SP_analysis.do"				// Analysis of robustness of SP result
do "B4_robustness/B4_wage_RDpermutation.do"			// Permutation test of wages

********************************************************
********************* IV ESTIMATION ********************
********************************************************
// E1: IV estimation
do "C1_IV/C1_prep_largesample.do"					// Prepare 2IV estimation sample
do "C1_IV/C1_RF_IV_estimates.do"					// RF and 2IV-estaimtes


********************************************************
******************* SEARCH ESTIMATION ******************
********************************************************
// D2: OLS
do "D2_OLS/D20_aku.do"
do "D2_OLS/D21_aku_prepare.do"
do "D2_OLS/D22_alanysis.do"

// D1: RKD
do "D1_RKD/D1_clean_iaf_payment.do"
do "D1_RKD/D2_search_unemp.do"
do "D1_RKD/D3_search_RAMS_wwage.do"
do "D1_RKD/D4_merge.do"
do "D1_RKD/D5_analysis_prepare.do"
do "D1_RKD/D6_analysis_ww.do"
do "D1_RKD/D7_analysis_v2.do"
do "D1_RKD/D8_bargraph.do"

********************************************************
******************** FIRM ESTIMATION *******************
********************************************************

//E1: Firm analysis
do "E1_firmanalysis/A1_definepop.do"
do "E1_firmanalysis/A2_rams.do"
do "E1_firmanalysis/A3_dj_not.do"
do "E1_firmanalysis/A4_dj_lifo.do"
do "E1_firmanalysis/A5_chi.do"
do "E1_firmanalysis/A6_outc.do"
do "E1_firmanalysis/A7_anaprep.do"
do "E1_firmanalysis/B1_Eventpanel.do"
do "E1_firmanalysis/C0_eventprep.do"
do "E1_firmanalysis/C1_eventana.do"
do "E1_firmanalysis/C3_graphs.do"








